<?php

namespace Aitels\Foundation\Dictionary;


use Illuminate\Support\Arr;

class Dictionary
{

    /**
     * 相当于空值
     */
    const NONE = 0;

    /**
     * 常量转为中文字符串
     * @param int $const
     * @return mixed
     */
    public static function toStr($const)
    {
        return static::MAP[$const] ?? false;
    }

    /**
     * 用常量名称字符串获取常量值
     */
    public static function matchByName($constName)
    {
        $reflection = new \ReflectionClass(static::class);
        return $reflection->getConstant($constName);
    }


    public static function matchByZhName($zhName)
    {
        return array_search($zhName, static::MAP);
    }

    /**
     * 用常量值获取常量名称字符串
     */
    public static function getConstName(int $const_value)
    {

        $reflection = new \ReflectionClass(static::class);
        return array_search($const_value, $reflection->getConstants());
    }

    /**
     * 获取所有定义的常量及其数值
     */
    public static function all()
    {
        $constants = (new \ReflectionClass(static::class))->getConstants();
        return Arr::except($constants, 'MAP');
    }

    /**
     * 将所有常量导出数组，一般用作前端下拉框
     * @return string
     */
    public static function exportAll()
    {
        return static::MAP;
    }

    public static function exportAllWithNone()
    {
        return static::MAP + [self::NONE => '无'];
    }

    /**
     * 将所有常量导出字符串，默认逗号分隔
     * @param string $glue
     * @return string
     */
    public static function exportImplodeAll($glue = ',')
    {
        return implode($glue, static::MAP);
    }

    /**
     * 根据名称获取常量值
     */
    public static function getConstantValuesByName($stringName)
    {

        $constants = (new \ReflectionClass(static::class))->getConstants()['MAP'];
        foreach ($constants as $index => $name) {
            if ($name == $stringName) {
                $type = $index;
            }
        }
        return $type;
    }

}
